package com.qf.collection.generic;

import com.qf.collection.Animal;
import com.qf.collection.Cat;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 演示泛型的用法
 */
public class Demo01 {
    public static void main(String[] args) {

        Collection<String> c1 = new ArrayList<String>();
        c1.add("hello");
//        c1.add(123);
//        c1.add(new Cat());
        c1.add("world");

        Iterator<String> iterator = c1.iterator();
        while (iterator.hasNext()){
            String s = iterator.next();
            System.out.println(s.toUpperCase());
        }

        System.out.println(c1);

        Collection<Animal> c2 = new ArrayList<>();
        c2.add(new Cat("波斯猫",1));
        c2.add(new Animal());
        Iterator<Animal> iterator1 = c2.iterator();
        while (iterator1.hasNext()) {
            Animal next = iterator1.next();
            // next转换为cat
            if (next instanceof Cat) {
                Cat c = (Cat) next;
                System.out.println(c.getName());
            }
        }

    }
}
